<?php
/*
 * @Descripttion: 
 * @version: 
 * @Author: Eugene
 * @Date: 2023-06-26 15:37:42
 * @LastEditors: Lk
 * @LastEditTime: 2023-07-06 15:24:49
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Category;
use app\common\model\goods\Cart;
use app\common\model\server\Goods as ServerGoods;
use app\common\model\server\Review;
use app\common\model\user\Level;

/**
 * 服务商品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 商品列表
     * @ApiMethod (GET)
     * @param string $category_id  分类id
     * @param string $keywords  搜索关键字
     */
    public function lists()
    {
        $params = request()->param();
        extract($params);
        bcscale(2);
        $goods = new ServerGoods;
        if (isset($category_id) && $category_id) {
            $goods->where('category_id', $category_id);
        }
        if (isset($keywords) && $keywords) {
            $goods->whereLike('title', '%' . $keywords . '%');
        }
        $user_id = $this->auth->id;
        $lists = $goods->with(['cart' => function ($qq) use ($user_id) {
            $qq->where('user_id', $user_id);
        }])->where('status', 'normal')->order('weigh desc,id desc')->paginate();

        $user = $this->auth->getUser();
        $level = Level::get($user->level_id);
        //会员优惠金额
        if ($user->level_id) {
            foreach ($lists as $v) {
                if ($level && $level->discount > 0) {
                    $discount = bcdiv($level->discount, 100);
                    $v->vip_deduction = bcmul($v->pay_money, $discount);
                    $v->vip_money = bcsub($v->pay_money,  $v->vip_deduction);
                }
            }
        }
        $this->success('请求成功', $lists);
    }

    /**
     * 商品详情
     * @ApiMethod (GET)
     * @param string $id  商品id
     */
    public function info()
    {
        $params = request()->param();
        extract($params);
        bcscale(2);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $data = ServerGoods::get(['id' => $id, 'status' => 'normal']);
        $user = $this->auth->getUser();
        //会员优惠金额
        if ($user->level_id) {
            $level = Level::get($user->level_id);
            if ($level && $level->discount > 0) {
                $discount = bcdiv($level->discount, 100);
                $data->vip_deduction = bcmul($data->pay_money, $discount);
                $data->vip_money = bcsub($data->pay_money,  $data->vip_deduction);
            }
        }

        $this->success('请求成功', $data);
    }
    /**
     * @Author LK
     * @ApiTitle (商品评论)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function goods_review()
    {
        $params = request()->param();
        extract($params);
        $list = Review::with(['user'])->where('goods_id', $id)->select();
        $this->success('请求成功', $list);
    }
}
